﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.EditOrderUC;
using Model.Entities;
using Nethouse.Lib.Mvp;

namespace SampleWeb.WUC
{
    public partial class Select : MvpUserControl<ISelectOrderView>, ISelectOrderView
    {
        public event MvpEventHandler CompanyChanged;
        public event MvpEventHandler OrderChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
            ddCompany.SelectedIndexChanged += delegate { ActivateEvent(CompanyChanged); };
            ddOrder.SelectedIndexChanged += delegate { ActivateEvent(OrderChanged); };

            if (!IsPostBack)
            {
            }
        }

        public IList<Company> CompanyList { set { setCompanyList(value); } }

        private void setCompanyList(IList<Company> companyList)
        {
            ddCompany.DataSource = companyList;
            ddCompany.DataBind();
            ddCompany.Items.Insert(0, new ListItem("<Välj kund>", "0"));
            ddCompany.SelectedIndex = 0;

        }

        public int CurrentCompany
        {
            set { setCompany(value); }
            get { return getCompany(); }
        }

        private void setCompany(int value)
        {
            ddCompany.SelectedValue = value.ToString(CultureInfo.InvariantCulture);
        }

        private int getCompany()
        {
            int result;
            if (int.TryParse(ddCompany.SelectedValue, out result) == false)
            {
                throw new ApplicationException("Id för företag saknas!");
            }

            return result;
        }


        public IList<Order> OrderList { set { setOrderList(value); } }

        private void setOrderList(IList<Order> orderList)
        {
            ddOrder.DataSource = orderList;
            ddOrder.DataBind();
            ddOrder.Items.Insert(0, new ListItem("<Välj order>", "0"));
            ddOrder.SelectedIndex = 0;
        }

        public int CurrentOrder
        {
            set { setOrder(value); }
            get { return getOrder(); }
        }


        private void setOrder(int value)
        {
            ddOrder.SelectedValue = value.ToString(CultureInfo.InvariantCulture);
        }

        private int getOrder()
        {
            int result;
            if (int.TryParse(ddOrder.SelectedValue, out result) == false)
            {
                throw new ApplicationException("Id för order saknas!");
            }

            return result;
        }

    }
}